package com.gxuwz.medical.web.servlet;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.alibaba.fastjson.JSON;
import com.gxuwz.medical.dao.BaoXiaoDao;
import com.gxuwz.medical.dao.PersonDao;
import com.gxuwz.medical.domain.baoxiao.BaoXiao;
import com.gxuwz.medical.domain.baoxiao.BaoxiaoStatistic;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.person.Person;
import com.gxuwz.medical.domain.policy.Policy;
import com.gxuwz.medical.vo.PageBean;

public class BaoxiaoServlet extends BaseServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		try {
			if(method.equals("list")){
				list(request,response);
			}else if(method.equals("swich")){
				swich(request,response);
			}else if(method.equals("serch")){
				serch(request,response);
			}else if(method.equals("calcu")){
				calcu(request,response);
			}else if(method.equals("add")){
				add(request,response);
			}else if(method.equals("setPolicy")){
				setPolicy(request,response);
			}
			else if(method.equals("statistic")){
				statistic(request,response);
			}
			else if(method.equals("getexcel")){
				getexcel(request,response);
			}
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		// TODO Auto-generated method stub
		String path = "/page/baoxiao/baoxiao_list.jsp";
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = new BaoXiaoDao().queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}
	private void calcu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		// TODO Auto-generated method stub
		String curemoneystr = request.getParameter("curemoney");
		String idcard = request.getParameter("idcard");
		double curemoney = Double.parseDouble(curemoneystr);
		BaoXiao bx = new BaoXiao();
		bx.setIdcard(idcard);
		bx.setCuremoney(curemoney);
		bx.countmoney();
		
		String resp = JSON.toJSONString(bx);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(resp);
	}
	private void swich(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		// TODO Auto-generated method stub
		String path = "/page/baoxiao/baoxiao_add_swich.jsp";
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = new PersonDao().queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}

	private void serch(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, SQLException {
		// TODO Auto-generated method stub
		String path = "/page/baoxiao/baoxiao_add_swich.jsp";
		PersonDao dao = new PersonDao();
		String page = request.getParameter("page");
		String idcard = request.getParameter("idcard");
		idcard = new String(idcard.getBytes("ISO-8859-1"),"UTF-8");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = dao.queryByIdcard(idcard,currentpage,5);
		request.setAttribute("pageBean", pageBean);
		if(pageBean.getPageData().size()==0)
			request.setAttribute("msg", "该身份证未参合");
		request.getRequestDispatcher(path).forward(request, response);
	}
	private void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, SQLException, ParseException {
		// TODO Auto-generated method stub
		String idcard = request.getParameter("idcard");
		String illname = request.getParameter("illname");
		String cureno = request.getParameter("cureno");
		String curetimestr = request.getParameter("curetime");
		String curemoneystr = request.getParameter("curemoney");
		String premoneystr = request.getParameter("premoney");
		String totalmoneystr = request.getParameter("totalmoney");
		String relmoneystr = request.getParameter("relmoney");
		double curemoney = Double.parseDouble(curemoneystr);
		double premoney = Double.parseDouble(premoneystr);
		double totalmoney = Double.parseDouble(totalmoneystr);
		double relmoney = Double.parseDouble(relmoneystr);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date curetime = df.parse(curetimestr);
		Date baoxiaotime = df.parse(df.format(new Date()));
		BaoXiao bx = new BaoXiao(idcard, illname, cureno, curemoney, curetime, premoney, totalmoney, relmoney, baoxiaotime);
		String msg = bx.checkInfo();
		if(msg.equals(""))
			new BaoXiaoDao().add(bx);
		else
			request.setAttribute("msg", msg);
		list(request, response);
	}

	private void setPolicy(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SQLException, ParseException{
		String toplinestr = request.getParameter("topline");
		String ratiostr = request.getParameter("ratio");
		double topline = Double.parseDouble(toplinestr);
		double ratio = Double.parseDouble(ratiostr);
		Policy p = new Policy(topline, ratio);
		if(request.getParameter("reset")!=null&&request.getParameter("reset").equals("yes"))
			p.edit();
		else
			p.add();
		list(request, response);
	}
	
	private void statistic(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SQLException, ParseException{
		String path="/page/baoxiao/baoxiao_statistic_list.jsp";
		String areacode = request.getParameter("areacode");
		String fullname = request.getParameter("fullname");
		String illname = request.getParameter("illname");
		String stimestr = request.getParameter("stime");
		String etimestr = request.getParameter("etime");

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date stime=null;
		Date etime=null;
		if(areacode==null){
			areacode="";
		}
		if(fullname==null){
			fullname="";
		}else{
			fullname = new String(fullname.getBytes("ISO-8859-1"),"UTF-8");
		}
		
		if(illname==null){
			illname="";
		}else{
			illname = new String(illname.getBytes("ISO-8859-1"),"UTF-8");
		}
		if(stimestr!=null&&!stimestr.equals("")){
			stime = df.parse(stimestr);
			}
		if(etimestr==null&&!etimestr.equals("")){
			etime = df.parse(etimestr);
			}

		BaoxiaoStatistic bs = new BaoxiaoStatistic();
		List<BaoxiaoStatistic> list =bs.statistic(fullname, illname, stime, etime, areacode);
		request.getSession().setAttribute("list", list);
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	private void getexcel(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SQLException, ParseException{
		OutputStream out = response.getOutputStream();
		String path ="/page/baoxiao/baoxiao_statistic_list.jsp";
		String msg = "";
		List list = (List)request.getSession().getAttribute("list");
		if(list!=null){
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
		HSSFSheet hssfSheet = hssfWorkbook.createSheet("sheet");
		int CurrentRow=0;
		int CurrentCell=0;
		HSSFRow row = hssfSheet.createRow(CurrentRow++);
		row.createCell(CurrentCell++).setCellValue("序号");
		row.createCell(CurrentCell++).setCellValue("报销编号");
		row.createCell(CurrentCell++).setCellValue("镇");
		row.createCell(CurrentCell++).setCellValue("村");
		row.createCell(CurrentCell++).setCellValue("组");
		row.createCell(CurrentCell++).setCellValue("身份证号");
		row.createCell(CurrentCell++).setCellValue("姓名");
		row.createCell(CurrentCell++).setCellValue("慢性病名称");
		row.createCell(CurrentCell++).setCellValue("实际报销金额");
		row.createCell(CurrentCell++).setCellValue("报销时间");
		for(int i=0;i<list.size();i++){
			BaoxiaoStatistic bs = (BaoxiaoStatistic)list.get(i);
			Person p = bs.getPerson();
			Family f = bs.getFamily();
			BaoXiao b = bs.getBaoxiao();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(p!=null&&f!=null&&b!=null){
				CurrentCell=0;
				row = hssfSheet.createRow(CurrentRow++);
				row.createCell(CurrentCell++).setCellValue(i+1);
				row.createCell(CurrentCell++).setCellValue(b.getBaoxiaono());
				row.createCell(CurrentCell++).setCellValue(f.getTown());
				row.createCell(CurrentCell++).setCellValue(f.getVillage());
				row.createCell(CurrentCell++).setCellValue(f.getGroup());
				row.createCell(CurrentCell++).setCellValue(p.getIdcard());
				row.createCell(CurrentCell++).setCellValue(p.getFullname());
				row.createCell(CurrentCell++).setCellValue(b.getIllname());
				row.createCell(CurrentCell++).setCellValue(b.getRelmoney());
				row.createCell(CurrentCell++).setCellValue(df.format(b.getBaoxiaotime()));
			}
		}
		
		hssfWorkbook.write(out);
		response.setContentType("octets/stream");
		response.addHeader("Content-Disposition", "attachment;filename=test.xls");
		}
		out.flush();
		out.close();
	}
}
